# python3.7
# -*- coding: utf-8 -*-
# @File     : doubanMusic.py
# @function : 爬取豆瓣音乐Top250前4页数据，并将数据存储到Excel文件中
# @Software : PyCharm Community Edition
# @Knowledge: lxml、写表格文件
# 爬取的信息有： 歌曲名、表演者、发行时间、流派、评分等

import requests
from lxml import etree
import xlwt
import time

# 将数据写入musicList.xls文件
columns = ["歌曲名", "表演者", "流派", "发行时间", "评分"]
excel_file_name = "musicInfo.xls"


def save_file(data):
    wb = xlwt.Workbook()
    ws = wb.add_sheet('Sheet1')

    # 写入表头
    for col_index, col_value in enumerate(columns):
        ws.write(0, col_index, col_value)

    # 写入数据
    for row_index, row_data in enumerate(data):
        infor = row_data.split('/')
        for col_index, col_value in enumerate(infor):
            ws.write(row_index + 1, col_index, col_value)

    wb.save(excel_file_name)
    print(f"数据已成功写入Excel文件：{excel_file_name}")


# 获取数据存入到musicList列表
def gain_data(urllist):
    result_data = []
    for url in urllist:
        response = requests.get(url)
        if response.status_code == 200:
            tree = etree.HTML(response.content)
            result = tree.xpath('//div[@class="pl2"]/p/text()')
            result_data.extend(result)
            # time.sleep(1)
        else:
            print(f"Failed to fetch data from {url}")
    print(result_data)
    return result_data


# 在控制台输出数据
def show_data(result):
    for i in result:
        print(i.split('/'))


# 主程序
if __name__ == "__main__":
    urllist = [
        'http://localhost:8080/douban/douban1.html',
        'http://localhost:8080/douban/douban2.html',
        'http://localhost:8080/douban/douban3.html',
        'http://localhost:8080/douban/douban4.html'
    ]

    data = gain_data(urllist)
    show_data(data)
    save_file(data)

"""
musicList = []
#将数据写入musicList.xls文件
columns = ["歌曲名","表演者","流派","发行时间", "评分"]

wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet1')
excel_file_name = "musicInfo.xls"
# 写入表头
for col_indexs, col_values in enumerate(columns):
    ws.write(0, col_indexs, col_values)

def save_file(data):
    for i in data:
        print("*"*20)
        infor = (i.split('/'))
        time.sleep(1)
        for col_indexs, col_values in enumerate(infor):
            ws.write(1, col_indexs, col_values)
    wb.save(excel_file_name)




#获取数据存入到musicList列表
def gain_data(urllist):
    # 补全代码
    for i in urllist:
        # print(i)

        response = requests.get(i)
        if response.status_code == 200:
            # print(response.content.decode('utf-8'))
            tree = etree.HTML(response.content)
            result = tree.xpath('//div[@class="pl2"]/p/text()')
            return result
        else:
            print("failed...")

#在控制台输出数据
def show_data(result):
    for i in result:
        print(i.split('/'))

#主程序
if __name__ == "__main__":
    urllist = ['http://localhost:8080/douban/douban1.html',
               'http://localhost:8080/douban/douban2.html',
               'http://localhost:8080/douban/douban3.html',
               'http://localhost:8080/douban/douban4.html']
    data = gain_data(urllist)
    show_data(data)
    save_file(data)
"""